

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class main {
	private static final int BUFFER_SIZE= 1024*1024*66;
	private Map<Integer, Result> ramHolder = new HashMap<Integer, Result>((BUFFER_SIZE/66)*2);
	private Map<Integer, Result> resultMap = new HashMap<Integer, Result>();
	private FileInputStream fis = null;
	private FileChannel fc = null;
	private ByteBuffer byteBuf = null;
	private List<Integer> searchList = new ArrayList<Integer>();
	private List<Result> resultListWithSameHashCodes = new ArrayList<Result>(); 
	class Result {
		Result(byte[] key, byte[] value) {
			this.key = key;
			this.value = value;
		}

		byte[] key = null;
		byte[] value = null;
		int hashcode = 0;
	}

	private int buildRamContentMap() throws IOException {
		byte b;
		int byteBufSize = 32;
		byte[] word = new byte[byteBufSize];
		byte[] key = null;
		byte[] value = null;
		int byteBufCount = 0;
		Result res = null;
		
		int bytesRead = fc.read(byteBuf);
		byteBuf.flip();
		while (byteBuf.hasRemaining()) {
			b = byteBuf.get();
			if (b == 44) {
				key = word;
				word = new byte[byteBufSize];
				byteBufCount = 0;
				continue;
			} else if (b == 13 || b == 10) {
				if (byteBufCount == 0) {
					continue;
				}
				value = word;
				word = new byte[byteBufSize];
				res = new Result(key, value);
				ramHolder.put(Arrays.hashCode(key), res);
				ramHolder.put(Arrays.hashCode(value), res);
				byteBufCount = 0;
				continue;
			}
			word[byteBufCount++] = b;
		}
		byteBuf.clear();
		return bytesRead;

	}
	private int getLastContent() throws IOException {
		byte b;
		int byteBufSize = 32;
		byte[] word = new byte[byteBufSize];
		byte[] key = null;
		byte[] value = null;
		int byteBufCount = 0;
		Result res = null;
		
		int bytesRead = fc.read(byteBuf);
		
			byteBuf.flip();
			while (byteBuf.hasRemaining()) {
				b = byteBuf.get();
				if (b == 44) {
					key = word;
					word = new byte[byteBufSize];
					byteBufCount = 0;
					continue;
				} else if (b == 13 || b == 10) {
					if (byteBufCount == 0) {
						continue;
					}
					value = word;
					word = new byte[byteBufSize];
					res = new Result(key, value);
					/******************************************************/
					int keyHashCode = Arrays.hashCode(key);
					int valHashCode = Arrays.hashCode(value);
					Result existingResult = ramHolder.get(Integer.valueOf(keyHashCode));
					if(existingResult != null){
//						existingResult.hashcode = keyHashCode;
//						resultListWithSameHashCodes.add(existingResult);
//						res.hashcode = keyHashCode;
//						resultListWithSameHashCodes.add(res);
						System.out.println("Key = " + new String(existingResult.key) + " value = " + new String(existingResult.value) + " HashCode = " + keyHashCode);
						System.out.println("Key = " + new String(res.key) + " value = " + new String(res.value) + " HashCode = " + keyHashCode);
					}
					existingResult = ramHolder.get(Integer.valueOf(valHashCode));
					if(existingResult != null){
//						existingResult.hashcode = valHashCode; 
//						resultListWithSameHashCodes.add(existingResult);
//						res.hashcode = valHashCode;
//						resultListWithSameHashCodes.add(res);
						System.out.println("Key = " + new String(existingResult.key) + " value = " + new String(existingResult.value) + " HashCode = " + valHashCode);
						System.out.println("Key = " + new String(res.key) + " value = " + new String(res.value) + " HashCode = " + valHashCode);
					}
					/******************************************************/
					ramHolder.put(Arrays.hashCode(key), res);
					ramHolder.put(Arrays.hashCode(value), res);
					byteBufCount = 0;
					continue;
				}
				word[byteBufCount++] = b;				
			}
			byteBuf.clear();
			
			

		return bytesRead;

	}
	private int findValues() {
		int count = 0;
		int size = searchList.size();
		List<Integer> removeList = new ArrayList<Integer>();
		for (Iterator<Integer> itr = searchList.iterator(); itr.hasNext();) {
			Integer id = itr.next();
			if (ramHolder.containsKey(id)) {
				count++;
				resultMap.put(id, ramHolder.get(id));
				removeList.add(id);
			}
			if (count == size) {
				break;
			}
		}
		searchList.removeAll(removeList);
		return count;
	}

	private void openConnection(String fileName) throws FileNotFoundException {

		this.fis = new FileInputStream(new File(fileName));
		this.fc = fis.getChannel();
		this.byteBuf = ByteBuffer.allocate(BUFFER_SIZE);
	}

	private void closeConnection() {
		try {
			if (fis != null) {
				fis.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		char[] cbuf;

		// This is just a demo. You can use any logic here to read from stdin -
		// till End of File or End of Stream etc.,
		cbuf = new char[1024];
		try {
			in.read(cbuf);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		main test = new main();

		String inputs = new String(cbuf);
		args = inputs.split(" ");
		if (args == null) {
			System.out.print("FAILURE");
			System.exit(1);
		}
		if (args.length == 1) {
			File f = new File(args[0].trim());
			if (f.exists()) {
				int bytesRead = 0;
				long end = 0;
				try {
					test.openConnection(args[0].trim());
					long start = System.nanoTime();
					while (bytesRead != -1) {
						bytesRead = test.buildRamContentMap();
						test.ramHolder.clear();
					}
					end = TimeUnit.NANOSECONDS.toSeconds((System.nanoTime() - start));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					test.closeConnection();
				}
				System.out.print("READY");
//				System.out.print(" in " + end + " secs.");
				System.exit(0);
			} else {
				System.out.print("FAILURE");
				System.exit(1);
			}
		}
		int idSize = 0;
		for (int i = 1; i < args.length; i++) {
			String arg = args[i].trim();
			if (arg.contains("T=") || arg.contains("R=")) {
				String ids = arg.substring(arg.indexOf("=") + 1);
				String[] idArr = ids.split(",");
				for (int j = 0; j < idArr.length; j++) {
					test.searchList.add(Arrays.hashCode(idArr[j].trim()
							.getBytes()));
				}
			}
		}
		idSize = test.searchList.size();
		File f = new File(args[0].trim());
		if (f.exists()) {
			int bytesRead = 0;
			long end = 0;
			try {
				test.openConnection(args[0].trim());
				long start = System.nanoTime();
				while (bytesRead != -1) {
					if(test.searchList.size() >0){
						bytesRead = test.buildRamContentMap();					
						test.findValues();
						test.ramHolder.clear();
					}	else	{
						break;
					}
				}
				end = TimeUnit.NANOSECONDS.toSeconds((System.nanoTime() - start));
				System.out.println("Ends in "+end+" Secs.");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				test.closeConnection();
			}
		} else {
			System.out.print("FAILURE");
			System.exit(1);
		}
		
		for (int i = 1; i < args.length; i++) {
			String arg = args[i].trim();
			if (arg.contains("T=") || arg.contains("R=")) {
				String ids = arg.substring(arg.indexOf("=") + 1);
				String[] idArr = ids.split(",");
				for (int j = 0; j < idArr.length; j++) {
					byte[] key = idArr[j].trim().getBytes();
					Integer keyHashCode = Integer.valueOf(Arrays.hashCode(key));
					Result res =test.resultMap.get(keyHashCode); 
					
					if (res != null) {
						byte[] id = res.key;
						if(keyHashCode.intValue() == Arrays.hashCode(id)){
							System.out.print(new String(res.value));
						}else{
							System.out.print(new String(id));
						}
					} else {
						System.out.print("FAILURE");
					}
					if ((idSize - 1) != 0) {
						System.out.print(',');
					}
					idSize--;
				}
			}
		}
		
	}
}
// E:\\doc\\contest\\comviva\\generate_random_file.perl\\1M.csv T=TXN00000000000000106523778875392,TXN00000000000000137146895695872 R=REF00000000000000025220047962112,REF00000000000000073924977098752
// E:\\doc\\contest\\comviva\\generate_random_file.perl\\30M.csv T=TXN00000000000000106523778875392,TXN00000000000000137146895695872 R=REF00000000000000025220047962112,REF00000000000000073924977098752